using System.Web.UI.WebControls;
using System.Data;
using System;
using StillVoiceCommon;
using TeamTime.Classes;
namespace TeamTime
{
    partial class GroupSecurity : System.Web.UI.Page
    {
        Security sec;

        
        //Dim adap As New SqlDataAdapter(sql, conn) 
        int UserGroupID = 1;
        protected System.Web.UI.WebControls.Label lblUserName;
        protected System.Web.UI.WebControls.TextBox txtUserID;
        //bool isEditing = false;
        #region " Web Form Designer Generated Code "

        //This call is required by the Web Form Designer. 
        [System.Diagnostics.DebuggerStepThrough()]
        private void InitializeComponent()
        {

        }
        protected System.Web.UI.WebControls.Label msg;
        protected System.Web.UI.WebControls.DropDownList ddSecurityItem;
        protected System.Web.UI.WebControls.DropDownList ddUserGroup;
        protected System.Web.UI.WebControls.DropDownList ddAccessType;

        protected DataSet dsUserGroups = new DataSet();
        protected DataSet dsSecurityItems = new DataSet();
        protected DataSet dsAccessTypes = new DataSet();
        protected DataSet dsGrid = new DataSet();


        private void Page_Init(object sender, System.EventArgs e)
        {
            //CODEGEN: This method call is required by the Web Form Designer 
            //Do not modify it using the code editor. 
            InitializeComponent();
            sec = new Security(sqlDB.getDBConnString());
            dsUserGroups = sec.GetUserGroups(false);
            dsSecurityItems = sec.GetSecurityItems();
            dsAccessTypes = sec.GetAccessTypes();
        }

        #endregion

        private void Page_Load(object sender, System.EventArgs e)
        {
            if (!Page.IsPostBack)
            {
              //  if (!sec.CanDo(Convert.ToInt32(Session["UserID"]), "Usersedit"))
                //TopMenu1.checkPagePermissions = true;
                //TopMenu1.pagePermissionsMsg = "You do not have rights to manage UserGroup security";
                ddlUserGroupID.DataSource = dsUserGroups;
                ddlUserGroupID.DataBind();
                ddlUserGroupID.SelectedIndex = UserGroupID;
                buildListboxes();
            }

            //if ((bool)Session["UserIsReadOnly"])
            //{
            //    ibtAdd.Enabled = false;
            //    ibtRemove.Enabled = false;
            //}
        }
        protected void ibtRemove_Click(object sender, System.EventArgs e) 
        { 
            UserGroup g = new UserGroup(sqlDB.getDBConnString(), Convert.ToInt64(Session["UserID"])); 
            g.load(ddlUserGroupID.SelectedValue, "ID",false); 
             
            foreach (ListItem item in this.lstYes.Items) { 
                if (item.Selected) { 
                    g.removeSecurityItem(Convert.ToInt64(item.Value)); 
                } 
            } 
            g = null; 
            buildListboxes(); 
        }
        protected void ibtAdd_Click(object sender, System.EventArgs e) 
        {
            UserGroup g = new UserGroup(sqlDB.getDBConnString(), Convert.ToInt64(Session["UserID"]));
            g.load(ddlUserGroupID.SelectedValue, "ID", false); 
            
            foreach (ListItem item in this.lstNo.Items)
            { 
                if (item.Selected) { 
                    g.addSecurityItem(Convert.ToInt64( item.Value)); 
                } 
            } 
            g = null; 
            buildListboxes(); 
        }
        private void buildListboxes()
        {
            string sql;
            sql = "SELECT si.ID, si.SecurityItem " + "FROM [Security].SecurityItem si " + "INNER JOIN Security.GroupSecurity ugs ON si.ID = ugs.SecurityItemID " + "where ugs.UserGroupID = " + ddlUserGroupID.SelectedValue + " order by si.SecurityItem ";
            Functions.listBoxPopulate(this.lstYes, sql, "ID", "SecurityItem");

            sql = "SELECT si.ID, si.SecurityItem " + "FROM [Security].SecurityItem si " + "LEFT JOIN Security.GroupSecurity ugs ON si.ID = ugs.SecurityItemID  " + "and ugs.UserGroupID = " + ddlUserGroupID.SelectedValue + " where ugs.ID is null" + " order by si.SecurityItem ";
            Functions.listBoxPopulate(this.lstNo, sql, "ID", "SecurityItem");

        }



        protected void ddlUserGroupID_SelectedIndexChanged(object sender, System.EventArgs e)
        {
            buildListboxes();
        }
    }

}